package com.qd.p2p.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.GoodsCategoryService;
import com.qdlc.p2p.biz.service.GoodsService;
import com.qdlc.p2p.biz.service.ReceivingInfoService;
import com.qdlc.p2p.biz.service.ScoreService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.exception.ScoreException;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.checkrule.ScoreBaseInfoRuleCheck;
import com.qdlc.p2p.dal.common.RuleCheckHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.Goods;
import com.qdlc.p2p.dal.dto.GoodsCategory;
import com.qdlc.p2p.dal.dto.Score;
import com.qdlc.p2p.dal.model.GoodsModel;

/**
 * 积分商城
 * @author xxx
 * @version 2.0
 * @date 2015年6月19日 下午5:22:18
 */
public class ScoreMallAction extends BaseAction<GoodsModel> implements ModelDriven<GoodsModel> {
	
	@Resource
	private ScoreService scoreService;
	@Resource
	private ReceivingInfoService receivingInfoService;
	@Resource
	private GoodsService goodsService;
	@Resource
	private GoodsCategoryService goodsCategoryService;

	/** 积分基本规则 **/
	private ScoreBaseInfoRuleCheck scoreBaseInfoRuleCheck = RuleCheckHelper.getValue(ScoreBaseInfoRuleCheck.class);
	
	private Map<String, Object> data;
	
	/**
	 * 商城主页                    
	 */
	@Action(value = "/member/score/mall/main", interceptorRefs = {@InterceptorRef("globalStack")})
	public String main(){
		Score score = scoreService.findByUserId(this.getSessionUserId());
		request.setAttribute("validScore", score.getValidScore());
		List<GoodsCategory> clist = goodsCategoryService.enableList();
		request.setAttribute("clist", clist);
		// 是否开启积分抽奖
		request.setAttribute("lotteryOpen", SystemConfigHelper.getValue(Nid.LOTTERY_OPEN));
		return "main";
	}

	/**
	 * 
	 * 商品列表展示 
	 * 
	 * @throws Exception
	 */
	@Action(value = "/member/score/mall/goodsList")
	public void goodsList() throws Exception {
		// 此处要查询的是通过审核 并且上架的
		model.setType(GoodsModel.UP_SHELVES);
		if (scoreBaseInfoRuleCheck.need_verify) {
			model.setStatus(GoodsModel.PASS_AUDIT);
		} else {
			model.setStatus(99);
		}
		model.setRows(scoreBaseInfoRuleCheck.show_goods_num);
		PageDataList<GoodsModel> page = goodsService.findPageList(model);
		data = new HashMap<String, Object>();
		data.put("data", page);
		printWebJson(getStringOfJpaObj(data));
	}

	/**
	 * 商品详情页         
	 */
	@Action(value = "/member/score/mall/detail")
	public String detail() {
		Goods goods = goodsService.findById(model.getId());
		if (goods == null || goods.getType() != GoodsModel.UP_SHELVES) {
			throw new ScoreException("当前商品不存在或已下架");
		}
		Score userScore = scoreService.findByUserId(this.getSessionUserId());
		request.setAttribute("userScore", userScore);
		request.setAttribute("goods", goods);
		int receiningInfoCount = receivingInfoService.count(getSessionUser());
		boolean receivingInfo = receiningInfoCount > 0 ? true : false;
		request.setAttribute("receivingInfo", receivingInfo);
		return "detail";
	}
	
	/**
	 * 商品详情页
	 * @return
	 */
	@Action(value = "/scoreMall/detail_*", interceptorRefs = {@InterceptorRef("globalStack")})
	public String goodsDetails(){
		long goodsId = this.getRequestGoodsId(request.getServletPath());
		Goods goods = goodsService.findById(goodsId);
		//List<GoodsPic> picList = goodsPicService.getGoodsPicByGoodsId(goodsId);
		Score userScore = scoreService.findByUserId(this.getSessionUserId());
		request.setAttribute("userScore", userScore);
		//request.setAttribute("picList", picList);
		request.setAttribute("goods", goods);
		request.setAttribute("category", goods.getGoodsCategory());
		return "detailPage";
	}
	
	/**
	 * 根据动态的URL获取商品ID
	 * @param url
	 * @return
	 */
	private long getRequestGoodsId(String url){
		String goodsId = url.substring(18).replace(".action","");
		return StringUtil.toLong(goodsId);
	}
}
